package com.webchat.search.controller;

import com.webchat.common.bean.APIResponseBean;
import com.webchat.common.bean.APIResponseBeanUtil;
import com.webchat.domain.vo.request.search.MilvusSearchRequestVO;
import com.webchat.rmi.search.VectorSearchEngineClient;
import com.webchat.search.service.voctor.AbstractMilvusService;
import com.webchat.search.service.voctor.MilvusServiceFactory;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class VectorSearchEngineController implements VectorSearchEngineClient {


    @Override
    public APIResponseBean<Boolean> createCollection(String biz) {
        MilvusServiceFactory.getService(biz).createCollection();
        return APIResponseBeanUtil.success(true);
    }

    @Override
    public APIResponseBean<Boolean> dropCollection(String biz) {
        MilvusServiceFactory.getService(biz).dropCollection();
        return APIResponseBeanUtil.success(true);
    }

    @Override
    public APIResponseBean<Object> search(String biz, MilvusSearchRequestVO milvusSearchReq) {
        String query = milvusSearchReq.getQuery();
        Assert.notNull(query, "query must not be null!");
        Integer topK = milvusSearchReq.getTopK();
        topK = topK == null ? 1 : topK;
        float score = milvusSearchReq.getScore();
        AbstractMilvusService milvusService = MilvusServiceFactory.getService(biz);
        Object result = milvusService.search(query, topK, score);
        return APIResponseBeanUtil.success(result);
    }
}
